package sanshui.system.trade.util;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.cglib.core.CollectionUtils;
import sanshui.system.trade.db.entity.TradeHisOpPos;
import sanshui.system.trade.factory.trade.strategy.StrategyCallType;
import sanshui.system.trade.model.PositionAccModel;
import sanshui.system.trade.model.TradeHisPositionModel;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

public class TradePositionUtil {

    public static Map<String, PositionAccModel> parsePosHisToHoldInT0(List<TradeHisOpPos> posList) {
        Map<String, PositionAccModel> codePosMap = new HashMap<>();
        for (TradeHisOpPos tradeHisOpPos : posList){
            PositionAccModel accModel;
            String targetCode = tradeHisOpPos.getTargetCode();
            if (!codePosMap.containsKey(targetCode)){
                accModel = new PositionAccModel();
                accModel.setTsCode(targetCode);
                accModel.setNumTotal(0L);
                accModel.setNumCanTrade(0L);
                accModel.setAccId(tradeHisOpPos.getAccId());
                accModel.setWorkId(tradeHisOpPos.getWorkerId());
                codePosMap.put(targetCode, accModel);
            } else {
                accModel = codePosMap.get(targetCode);
            }
            String opType = tradeHisOpPos.getOpType();
            Long tradeNum = tradeHisOpPos.getTradeNum();
            if (StrategyCallType.BUY.name().equals(opType)){
                accModel.setNumTotal(accModel.getNumTotal() + tradeNum);
            }
            if (StrategyCallType.SELL.name().equals(opType)){
                if (accModel.getNumCanTrade() >= tradeNum){
                    accModel.setNumTotal(accModel.getNumTotal() - tradeNum);
                    accModel.setNumCanTrade(accModel.getNumCanTrade() - tradeNum);
                }
            }
            codePosMap.put(targetCode, accModel);
        }
        for (PositionAccModel value : codePosMap.values()) {
            if (0 == value.getNumTotal()){
                codePosMap.remove(value.getTsCode());
            }
        }
        return codePosMap;
    }
        /**
         * 将数据库中的历史交易信息，更新总的数量和可交易数量，T1
         * */
    public static Map<String, PositionAccModel> parsePosHisToHoldInT1(Map<String, PositionAccModel> codePosMap, List<TradeHisOpPos> posList, String datetime16){
        LocalDateTime currentTime;

        try {
            currentTime = CommonTimeUtil.getTimestamp(datetime16);
        } catch (Exception e){
            currentTime = LocalDateTime.now();
        }

        for (TradeHisOpPos tradeHisOpPos : posList) {
            String targetCode = tradeHisOpPos.getTargetCode();
            PositionAccModel accModel;
            if (!codePosMap.containsKey(targetCode)){
                accModel = new PositionAccModel();
                accModel.setTsCode(targetCode);
                accModel.setNumTotal(0L);
                accModel.setNumCanTrade(0L);
                accModel.setAccId(tradeHisOpPos.getAccId());
                accModel.setWorkId(tradeHisOpPos.getWorkerId());
                codePosMap.put(targetCode, accModel);
            } else {
                accModel = codePosMap.get(targetCode);
            }

            String opType = tradeHisOpPos.getOpType();
            Long tradeNum = tradeHisOpPos.getTradeNum();
            if (StrategyCallType.BUY.name().equals(opType)){
                accModel.setNumTotal(accModel.getNumTotal() + tradeNum);
                accModel.setBuyOpNum(accModel.getBuyOpNum() + 1);
                accModel.setEntryPrice(tradeHisOpPos.getTradePrice());
                String opTime = tradeHisOpPos.getOpTime();
                LocalDateTime opTime8 = CommonTimeUtil.getTimestamp8(opTime);
                if (CommonTimeUtil.compare(opTime8, currentTime, 1L, ChronoUnit.DAYS)) {
                    accModel.setNumCanTrade(accModel.getNumCanTrade() + tradeNum);
                }
            }

            if (StrategyCallType.SELL.name().equals(opType)){
                if (accModel.getNumCanTrade() >= tradeNum){
                    accModel.setNumTotal(accModel.getNumTotal() - tradeNum);
                    accModel.setSellOpNum(accModel.getSellOpNum() + 1);
                    accModel.setNumCanTrade(accModel.getNumCanTrade() - tradeNum);
                }
            }

            codePosMap.put(targetCode, accModel);
        }

//        for (PositionAccModel value : codePosMap.values()) {
//            if (0 == value.getNumTotal()){
//                codePosMap.remove(value.getTsCode());
//            }
//        }
        return codePosMap;
    }
    public static List<TradeHisPositionModel> getPositionsByCode(List<TradeHisPositionModel> positions, String code){
        List<TradeHisPositionModel> positionList = new ArrayList<>();
        for (TradeHisPositionModel position : positions) {
            if (code.equalsIgnoreCase(position.getTargetCode())){
                positionList.add(position);
            }
        }
        return positionList;
    }

    public static PositionAccModel getHoldPositionsByCode(List<PositionAccModel> positions, String code){
        for (PositionAccModel position : positions) {
            if (code.equalsIgnoreCase(position.getTsCode())){
                return position;
            }
        }

        PositionAccModel position = new PositionAccModel();
        position.setNumCanTrade(0L);
        position.setTsCode(code);
        position.setNumTotal(0L);
        return position;
    }
}
